package besta.moon.engine.selector.result;

import java.sql.*;

import besta.moon.sql.Expression;
import besta.moon.MoonException;
import besta.moon.MoonConnection;
import besta.moon.command.CommandSelect;
import besta.moon.engine.View;
import besta.moon.engine.ViewTable;
import besta.moon.sql.datatype.MutableNumeric;
import besta.moon.sql.expression.Expressions;

public class ViewResult extends TableViewResult {

    final private ViewTable view;
    final private Expressions columnExpressions;
    final private CommandSelect commandSelect;

    public ViewResult(ViewTable view) {
        this.view = view;
        this.columnExpressions = view.commandSelect.columnExpressions;
        this.commandSelect = view.commandSelect;
    }

    public ViewResult(MoonConnection con, CommandSelect commandSelect) throws SQLException {
        try {
            this.view = new ViewTable(con, commandSelect);
            this.columnExpressions = commandSelect.columnExpressions;
            this.commandSelect = commandSelect;
        } catch (Exception e) {
            throw MoonException.createFromException(e);
        }
    }

    @Override
    public boolean init(MoonConnection con) throws Exception {
        if (super.init(con)) {
            commandSelect.compile(con);
            return true;
        }
        return false;
    }

    @Override
    public View getTableView() {
        return view;
    }

    @Override
    public void deleteRow() throws SQLException {
        commandSelect.deleteRow(con);
    }

    @Override
    public void updateRow(Expression[] updateValues) throws Exception {
        commandSelect.updateRow(con, updateValues);
    }

    @Override
    public void insertRow(Expression[] updateValues) throws Exception {
        commandSelect.insertRow(con, updateValues);
    }

    @Override
    public boolean isNull(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).isNull();
    }

    @Override
    public boolean getBoolean(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).getBoolean();
    }

    @Override
    public int getInt(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).getInt();
    }

    @Override
    public long getLong(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).getLong();
    }

    @Override
    public float getFloat(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).getFloat();
    }

    @Override
    public double getDouble(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).getDouble();
    }

    @Override
    public long getMoney(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).getMoney();
    }

    @Override
    public MutableNumeric getNumeric(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).getNumeric();
    }

    @Override
    public Object getObject(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).getObject();
    }

    @Override
    public String getString(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).getString();
    }

    @Override
    public byte[] getBytes(int colIdx) throws Exception {
        return columnExpressions.get(colIdx).getBytes();
    }

    @Override
    public int getDataType(int colIdx) {
        return columnExpressions.get(colIdx).getDataType();
    }

    @Override
    public void beforeFirst() throws Exception {
        commandSelect.beforeFirst();
    }

    @Override
    public boolean isBeforeFirst() throws SQLException {
        return commandSelect.isBeforeFirst();
    }

    @Override
    public boolean isFirst() throws SQLException {
        return commandSelect.isFirst();
    }

    @Override
    public boolean first() throws Exception {
        return commandSelect.first();
    }

    @Override
    public boolean previous() throws Exception {
        return commandSelect.previous();
    }

    @Override
    public boolean next() throws Exception {
        return commandSelect.next();
    }

    @Override
    public boolean last() throws Exception {
        return commandSelect.last();
    }

    @Override
    public boolean isLast() throws Exception {
        return commandSelect.isLast();
    }

    @Override
    public boolean isAfterLast() throws Exception {
        return commandSelect.isAfterLast();
    }

    @Override
    public void afterLast() throws Exception {
        commandSelect.afterLast();
    }

    @Override
    public boolean absolute(int row) throws Exception {
        return commandSelect.absolute(row);
    }

    @Override
    public boolean relative(int rows) throws Exception {
        return commandSelect.relative(rows);
    }

    @Override
    public int getRow() throws Exception {
        return commandSelect.getRow();
    }

    @Override
    public long getRowPosition() {
        return commandSelect.from.getRowPosition();
    }

    @Override
    public void setRowPosition(long rowPosition) throws Exception {
        commandSelect.from.setRowPosition(rowPosition);
    }

    @Override
    public final boolean rowInserted() {
        return commandSelect.from.rowInserted();
    }

    @Override
    public final boolean rowDeleted() {
        return commandSelect.from.rowDeleted();
    }

    @Override
    public void nullRow() {
        commandSelect.from.nullRow();

    }

    @Override
    public void noRow() {
        commandSelect.from.noRow();
    }

    @Override
    public final void execute() throws Exception {
        commandSelect.from.execute();
    }
}
